#include<bits/stdc++.h>
using namespace std;
class un
{
    vector<int> f;
public:
    int c;
    un(int n)
    {
        c=n;
        f=vector<int>(n);
        for(int i=0;i<n;i++)
            f[i]=i;
    }
    int find(int x)
    {
        if(f[x]!=x)
            f[x]=find(f[x]);
        return f[x];
    }
    bool merge(int x,int y)
    {
        int a=find(x),b=find(y);
        if(a!=b)
        {
            f[a]=b;
            c--;
            return true;
        }
        return false;
    }
};

class Solution {
public:
    int minCostConnectPoints(vector<vector<int>>& points) {
        int n=points.size();
        un ui(n);
        vector<tuple<int,int,int>> g;
        for(int i=0;i<n;i++)
        {
            int a=points[i][0],b=points[i][1];
            for(int j=i+1;j<n;j++)
            {
                int c=points[j][0],d=points[j][1];
                g.emplace_back(i,j,abs(a-c)+abs(b-d));
            }
        }
        int ans=0;
        sort(g.begin(),g.end(),[](tuple<int,int,int> t1,tuple<int,int,int> t2)
        {
            return get<2>(t1)<get<2>(t2);
        });
        for(auto& [x,y,w]:g)
        {
            if(ui.merge(x,y))
                ans+=w;
        }
        return ans;
    }
};

int main()
{
    vector<vector<int>> vec={{0,0},{2,2},{3,10},{5,2},{7,0}};
    Solution so;
    so.minCostConnectPoints(vec);
    return 0;
}